﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MazeNetGUI
{
    class BoardPanel : Panel
    {
        private boardType _mBoard;
        private CardPanel[,] _mCardPanels;

        /// <summary>
        /// Board, das angezeigt wird
        /// </summary>
        public boardType Board
        {
            get { return _mBoard; }
            set
            {
                _mBoard = value;
                if (value != null)
                {
                    for (int i = 0; i < 7; ++i)
                    {
                        for (int j = 0; j < 7; ++j)
                        {
                            _mCardPanels[i, j].Card = value.Arr[i, j];
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Event wird aufgerufen, wenn eine Karte geklickt wird
        /// </summary>
        public EventHandler<EventArgs> OnCardClicked;

        public BoardPanel()
            : base()
        {
            Init();
        }

        private void Init()
        {

            //Kartenpanel anlegen
            _mCardPanels = new CardPanel[7, 7];
            for (int i = 0; i < 7; ++i)
            {
                for (int j = 0; j < 7; ++j)
                {
                    _mCardPanels[i, j] = new CardPanel();
                    this.Controls.Add(_mCardPanels[i, j]);
                    _mCardPanels[i, j].Click += CardClicked;
                    _mCardPanels[i, j].MouseDown += CardMouseDown;
                    _mCardPanels[i, j].MouseUp += CardMouseUp;
                }
            }

            OnResize(new EventArgs());
        }

        /// <summary>
        /// Bearbeitet Groessenaenderung von Panel.
        /// </summary>
        /// <param name="eventargs"></param>
        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);

            int _lCardPanelSize;
            Point _lStartingPoint;

            //Panel neu Anordnen
            if (this.Height <= this.Width)
            {
                _lCardPanelSize = this.Height / 7;
                _lStartingPoint = new Point((this.Width - this.Height) / 2);
            }
            else
            {
                _lCardPanelSize = this.Width / 7;
                _lStartingPoint = new Point((this.Height - this.Height) / 2);
            }

            //Panelgroeßen Aendern
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    _mCardPanels[i, j].Width = _lCardPanelSize;
                    _mCardPanels[i, j].Height = _lCardPanelSize;
                    _mCardPanels[i, j].Location = new Point(_lStartingPoint.X + _lCardPanelSize * j, _lStartingPoint.Y + _lCardPanelSize * i);
                }
            }

            this.Refresh();
        }

        /// <summary>
        /// Wird aufgerufen, wenn Karte geklickt wird
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CardClicked(object sender, EventArgs e)
        {
            if (OnCardClicked != null)
            {
                OnCardClicked(sender, e);
            }
        }

        /// <summary>
        /// Reagiert auf das MouseDownEvent der Karten,
        /// indem es erreichbare Felder gruen einfaerbt.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CardMouseDown(object sender, MouseEventArgs e)
        {
            Graphics g;
            bool[,] _lReachableFields;
            Point _lClickPos = new Point(-1,-1);

            if (_mBoard == null)
            {
                return;
            }

            //Richtige Karte suchen
            for (int i = 0; i < 7; ++i)
            {
                for (int j = 0; j < 7; ++j)
                {
                    if (_mCardPanels[i, j].Equals(sender))
                    {
                        _lClickPos = new Point(j,i);
                        break;
                    }
                }
            }

            //Erreichbare Felder suchen
            _lReachableFields = _mBoard.ReachableFields(_lClickPos);

            //einfaerben
            for (int i = 0; i < 7; ++i)
            {
                for (int j = 0; j < 7; ++j)
                {
                    if (_lReachableFields[i, j])
                    {
                        _mCardPanels[i, j].SetReachable();
                    }
                }
            }
        }

        /// <summary>
        /// Reagiert auf das MouseUpEvent der Karten,
        /// indem es Refreshed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CardMouseUp(object sender, MouseEventArgs e)
        {
            Refresh();
        }
    }
}
